package com.jason.um.user.model;

import com.jason.um.user.entity.User;
import com.jason.um.user.entity.UserSign;
import lombok.Data;
import org.hibernate.validator.constraints.Length;
import org.springframework.beans.BeanUtils;

import javax.validation.constraints.*;


@Data
public class UserModel {

    private Integer id;

    @Length(min = 2, max = 10)
    @NotBlank
    private String name;

    @DecimalMin("0")
    @DecimalMax("150")
    private Integer age;

    @Email
    private String email;

    @NotBlank
    private String telephone;

    private Byte gender;

    private String roles;

    private String regType = "byPhone";

    private String regAccount;

    @NotBlank
    @Length(min = 2, max = 10)
    private String password;

    private String avatar;

    public static UserModel  convertFromUserEntity(User user, UserSign userSign) {
        if (user == null){
            return null;
        }
        UserModel userModel = new UserModel();
        BeanUtils.copyProperties(user, userModel);
        if (userSign != null){
            userModel.setPassword(userSign.getPassword());
        }
        return userModel;
    }

    public User convertToUser() {
        User user = new User();
        BeanUtils.copyProperties( this, user);
        return user;
    }
    public UserSign convertToUserSign() {
        UserSign userSign = new UserSign();
        userSign.setPassword(this.password);
        userSign.setUserId(this.id);
        return userSign;
    }

}
